package q1.project.core.exception;

/** 错误码枚举 参考 Graceful Response 的设计理念，统一管理错误码 */
public enum ErrorCode {

  // ==================== 通用错误码 ====================
  SUCCESS(1, "操作成功"),
  FAIL(0, "操作失败"),
  SYSTEM_ERROR(-1, "未处理异常"),

  // ==================== 认证相关错误码 ====================
  NOT_LOGIN(1001, "未登录或登录已过期"),
  NOT_TOKEN(1002, "未提供Token"),
  INVALID_TOKEN(1003, "Token无效"),
  TOKEN_TIMEOUT(1004, "Token已过期"),
  TOKEN_BE_REPLACED(1005, "Token已被顶下线"),
  TOKEN_KICK_OUT(1006, "Token已被踢下线"),
  LOGIN_FAILED(1007, "登录失败"),
  USERNAME_OR_PASSWORD_ERROR(1008, "用户名或密码错误"),

  // ==================== 权限相关错误码 ====================
  PERMISSION_DENIED(2001, "权限不足"),
  ROLE_DENIED(2002, "角色不足"),

  // ==================== 参数校验错误码 ====================
  PARAM_VALIDATION_FAILED(3001, "参数验证失败"),
  PARAM_BIND_FAILED(3002, "参数绑定失败"),
  PARAM_MISSING(3003, "参数缺失"),

  // ==================== 业务错误码 ====================
  USER_NOT_FOUND(4001, "用户不存在"),
  USER_ALREADY_EXISTS(4002, "用户已存在"),
  USER_USERNAME_EXISTS(4003, "用户名已存在"),
  USER_EMAIL_EXISTS(4004, "邮箱已存在"),
  USER_ACCOUNT_DISABLED(4005, "账户已禁用"),
  USER_ACCOUNT_LOCKED(4006, "账户已锁定"),
  TENANT_NOT_FOUND(4007, "租户不存在"),
  RESOURCE_NOT_FOUND(4008, "资源不存在"),

  // ==================== 数据相关错误码 ====================
  DATA_NOT_FOUND(5001, "数据不存在"),
  DATA_ALREADY_EXISTS(5002, "数据已存在"),
  DATA_OPERATION_FAILED(5003, "数据操作失败"),

  // ==================== 业务通用错误码 ====================
  BUSINESS_ERROR(6000, "业务处理失败");

  private final Integer code;
  private final String message;

  ErrorCode(Integer code, String message) {
    this.code = code;
    this.message = message;
  }

  public Integer getCode() {
    return code;
  }

  public String getMessage() {
    return message;
  }

  /** 根据错误码获取枚举 */
  public static ErrorCode getByCode(Integer code) {
    for (ErrorCode errorCode : values()) {
      if (errorCode.getCode().equals(code)) {
        return errorCode;
      }
    }
    return null;
  }
}
